//
//  WEXVisitorView.m
//  microBlog_oc
//
//  Created by apple on 16/4/18.
//  Copyright © 2016年 Hill. All rights reserved.
//

//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS

#import <Masonry/Masonry.h>

#import "WEXVisitorView.h"

@interface WEXVisitorView ()

//添加圆圈
@property (nonatomic, weak) UIImageView *igv_circle;

//添加遮罩
@property (nonatomic, weak) UIImageView *igv_backView;

//添加中间图片
@property (nonatomic, weak) UIImageView *igv_iconView;

//添加文字
@property (nonatomic, weak) UILabel *lb_message;

//添加注册按钮
@property (nonatomic, weak) UIButton *btn_register;

//添加登录按钮
@property (nonatomic, weak) UIButton *btn_login;


@end

@implementation WEXVisitorView

-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    //添加子控件
    [self initWithSubViews];
    
    //设置子控件frame
    [self setupSubViews];
    
    
}

# pragma mark -
# pragma mark 添加子控件
- (void)initWithSubViews {
    
    //设置背景颜色
    self.backgroundColor = [UIColor colorWithWhite:237 / 255.0 alpha:1];
    
    
    //添加圆圈
    UIImageView *igv_circle = [[UIImageView alloc]init];
    self.igv_circle = igv_circle;
    igv_circle.hidden = YES;
    igv_circle.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
    [self addSubview:igv_circle];
    
    
    //添加遮罩
    UIImageView *igv_backView = [[UIImageView alloc] init];
    
    self.igv_backView = igv_backView;
    
    igv_backView.image = [UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"];
    
    [self addSubview:igv_backView];
    
   
    //添加中间图片
    UIImageView *igv_iconView = [[UIImageView alloc] init];
    
    self.igv_iconView = igv_iconView = [[UIImageView alloc] init];
    
    igv_iconView.image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
    
    [self addSubview:igv_iconView];
    
    
    //添加文字
    UILabel *lb_message = [[UILabel alloc] init];
    
    self.lb_message = lb_message;
    
    lb_message.text = @"世界这么大，可以去看看，微博这么精彩，可以来逛逛";
    lb_message.textColor = [UIColor darkGrayColor];
    lb_message.font = [UIFont systemFontOfSize:16];
    lb_message.preferredMaxLayoutWidth = 230;
    lb_message.textAlignment = NSTextAlignmentCenter;
    lb_message.numberOfLines = 0;
    
    [self addSubview:lb_message];
    
    
    //添加注册按钮
    UIButton *btn_register = [[UIButton alloc] init];
    
    self.btn_register = btn_register;
    
    [btn_register addTarget:self action:@selector(didClcikBtnRegiste:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn_register setBackgroundImage:[UIImage imageNamed:@"common_button_white"] forState:UIControlStateNormal];
    [btn_register setTitle:@"注册" forState:UIControlStateNormal];
    [btn_register setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    btn_register.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [self addSubview:btn_register];
    
    
    //添加登录按钮
    UIButton *btn_login = [[UIButton alloc] init];
    
    self.btn_login = btn_login;
    
    [btn_login addTarget:self action:@selector(didClickBtnLogin:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn_login setBackgroundImage:[UIImage imageNamed:@"common_button_white"] forState:UIControlStateNormal];
    [btn_login setTitle:@"登录" forState:UIControlStateNormal];
    [btn_login setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    btn_login.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [self addSubview:btn_login];
    
}


# pragma mark -
# pragma mark 设置子控件的约束
- (void)setupSubViews {
    
    //添加圆圈约束
    [self.igv_circle makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(self);
        
    }];
    
    //添加遮罩约束
    [self.igv_backView makeConstraints:^(MASConstraintMaker *make) {
       
        make.center.equalTo(self);
        
    }];
    
    //添加中间图片
    [self.igv_iconView makeConstraints:^(MASConstraintMaker *make) {
       
        make.center.equalTo(self);
        
    }];
    
    //添加文字
    [self.lb_message makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.igv_circle.mas_centerX);
        
        make.top.equalTo(self.igv_circle.mas_bottom).offset(15);
        
    }];
    
    
    //添加注册按钮
    [self.btn_register makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(self.lb_message.mas_leading);
        
        make.top.equalTo(self.lb_message.mas_bottom).offset(15);
        
        make.width.equalTo(100);
        
    }];
    
    //添加登录按钮
    [self.btn_login makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.equalTo(self.lb_message.mas_trailing);
        
        make.top.equalTo(self.btn_register.mas_top);
        
        make.width.equalTo(100);
        
    }];
    
}

# pragma mark -
# pragma mark 页面数据加载
- (void)setVistorInfoWithMessage:(NSString *)message iconImage:(NSString *)imgName {
    if (message != nil && imgName != nil) {
        
        self.igv_iconView.image = [UIImage imageNamed:imgName];
        
        self.lb_message.text = message;
        
    }else{
        
        [self setCircleAnima];
        
    }
    
}


# pragma mark -
# pragma mark 实现圆圈方法
- (void)setCircleAnima{
    
    self.igv_circle.hidden = NO;
    
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    //滚动弧度
    anim.toValue = @(2 * M_PI);
    //重复次数
    anim.repeatCount = MAXFLOAT;
    //旋转一圈需要的时间
    anim.duration = 20.0;
    //切换后台是否需要停止动画
    anim.removedOnCompletion = NO;
    
    [self.igv_circle.layer addAnimation:anim forKey:nil];

    
}

# pragma mark -
# pragma mark 按钮点击事件的相应

- (void)didClcikBtnRegiste:(UIButton *)button {
    
    if ([self.delegate respondsToSelector:@selector(VisitorView:withRegisteButton:)]) {
        [self.delegate VisitorView:self withRegisteButton:button];
    }
    
    
}

- (void)didClickBtnLogin:(UIButton *)button {
    
    if ([self.delegate respondsToSelector:@selector(VisitorView:withLoginButton:)]) {
        [self.delegate VisitorView:self withLoginButton:button];
    }
    
}

@end
